layui.use(['form'], function () {
    var $ = layui.jquery;
    var form = layui.form,
        layer = layui.layer;

    /**
     * 初始化表单，要加上，不然刷新部分组件可能会不加载
     */
    form.render();

    //获取验证码
    $("#imgVerify").on("click", function(){
        getVerify();
    });
    function getVerify(){
        $("#imgVerify").attr("src", '/sys/login/getVerify?' + Math.random());
    }

    // 进行登录操作
    form.on('submit(login)', function (data) {
        data = data.field;
        if (data.userName == '') {
            layer.msg('用户名不能为空', {icon: 5});
            return false;
        }
        if (data.password == '') {
            layer.msg('密码不能为空', {icon: 5});
            return false;
        }
        if (data.captcha == '') {
            layer.msg('验证码不能为空', {icon: 5});
            return false;
        }

        var index = layer.load(2);
        var url = "/sys/login/loginVerify";
        $.post(url, data, function(res){
            layer.close(index);
            if(res.state == "200"){ //登录成功
                layer.msg(res.message, {icon: 6}, function () {
                    location.href = '/';
                });
            }else{
                layer.msg(res.message, {icon: 5});
                $("#password").val(""); //清空密码
                getVerify(); //刷新验证码
            }
        });
        return false;
    });
});